package org.freespace.controller;


import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.freespace.api.JmeterDriver;
import org.freespace.model.User;
import org.freespace.utils.SessionUtil;
import org.freespace.utils.response.CommonResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * <p>
 * 商品信息 前端控制器
 * </p>
 *
 * @author astupidcoder
 * @since 2021-08-21
 */
@Slf4j
@RequestMapping("/login")
@Controller
public class LoginController {

    @PostMapping("/in")
    public synchronized CommonResult loginIn(@RequestBody Map<String, String> req, HttpSession session) {
        String userName = req.get("userName");

        User user = null;
        if (StringUtils.isNotBlank(userName)) {
            user = getUser(userName);
        } else {
            // throw
        }
        session.setAttribute(SessionUtil.USER_REDIS_KEY, user);
        session.setAttribute(SessionUtil.ADMIN_REDIS_KEY, isAdmin(user));
        return CommonResult.success();
    }

    private boolean isAdmin(User user) {
        // query mysql roletable
        return true;
    }

    private User getUser(String userName) {
        // query mysql user table
        return null;
    }
}
